<title>活动管理</title>
  
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>活动管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="layadmin-salefront-formlist">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">活动名称</label>
          <div class="layui-input-block">
            <input type="text" name="title" placeholder="请输入" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">活动类型</label>
          <div class="layui-input-block">
            <select name="paytype">
              <option value="">不限</option>
              <option value="0">卡券模式</option>
              <option value="1">纯推广模式</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">活动状态</label>
          <div class="layui-input-block">
            <select name="status">
              <option value="">全部状态</option>
              <option value="1">进行中</option>
              <option value="0">已结束</option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-sale" lay-submit lay-filter="LAY-user-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>
    
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <!-- <button class="layui-btn layui-btn-danger layuiadmin-btn-sale" data-type="batchdel">删除</button> -->
        <button class="layui-btn layuiadmin-btn-sale" data-type="add">添加活动</button>
      </div>
      
      <table id="LAY-sale-manage" lay-filter="LAY-sale-manage"></table>
      <script type="text/html" id="paytypeTpl">
        {{# if(d.paytype == 0){ }}
        <span class="layui-badge layui-bg-orange">卡券模式</span>
        {{# }else{ }}
        <span class="layui-badge layui-bg-blue">纯推广模式</span>
        {{# } }}
      </script>
      <script type="text/html" id="cardTpl">
        {{# var card = JSON.parse(d.card) }}
        {{# if(card.list){ }}
          {{# var cardname=[] }}
          {{# for(let i=0; i<card.list.length; i++){ }}
            {{ cardname[i] = card.list[i].name }}
          {{# } }}
          {{# cardname.join() }}
        {{# }else{ }}
          {{ card.name }}
        {{# } }}
      </script> 
      <script type="text/html" id="statusTpl">
        {{# var date = Date.parse(new Date())/1000 }}
        {{# if(parseInt(d.endtime)+86400 < date){ }}
        <span class="layui-badge layui-bg-black">已结束</span>
        {{# }else{ }}
        <span class="layui-badge layui-bg-green">进行中</span>
        {{# } }}
      </script> 
      <script type="text/html" id="table-sale-oper">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
<!--        <a class="layui-btn layui-btn-xs" lay-event="copy"><i class="layui-icon layui-icon-file"></i>复制</a>-->
        {{# if(d.paytype == 0){ }}<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="paytype0"><i class="layui-icon layui-icon-username"></i>核销员</a>{{# } }}
<!--        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="tuiguang"><i class="layui-icon layui-icon-release"></i>推广</a>-->
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        <a class="layui-btn layui-btn-warm layui-btn-xs more" lay-event="more" onmouseenter="this.click()"><i class="layui-icon layui-icon-down"></i>更多</a>
        <a class="layui-btn layui-btn-xs layui-hide" lay-event="lqjl" id="lqjl{{d.id}}"><i class="layui-icon layui-icon-username"></i>领券记录</a>
        <a class="layui-btn layui-btn-xs layui-hide" lay-event="cyyh" id="cyyh{{d.id}}"><i class="layui-icon layui-icon-username"></i>参与用户</a>
        <a class="layui-btn layui-btn-xs layui-hide" lay-event="hdsj" id="hdsj{{d.id}}"><i class="layui-icon layui-icon-username"></i>活动数据</a>
        <a class="layui-btn layui-btn-xs layui-hide" lay-event="cjspjl" id="cjspjl{{d.id}}"><i class="layui-icon layui-icon-username"></i>合成视频记录</a>
        <a class="layui-btn layui-btn-xs layui-hide" lay-event="sclb" id="sclb{{d.id}}"><i class="layui-icon layui-icon-username"></i>素材列表</a>
      </script>
    </div>
  </div>
</div>

<ul class="btnbox">
  <li><a href="javascript:;" id="lqjl">领券记录</a></li>
  <li><a href="javascript:;" id="cyyh">参与用户</a></li>
  <li><a href="javascript:;" id="hdsj">活动数据</a></li>
  <li><a href="javascript:;" id="jshd">结束活动</a></li>
  <li><a href="javascript:;" id="cjspjl">合成视频记录</a></li>
  <li><a href="javascript:;" id="sclb">素材列表</a></li>
</ul>
<style>
  .btnbox{
    min-width: 4em;
    padding: 7px 0;
    background-color: #FFF;
    position: absolute;
    top: 275px;
    right: 55px;
    z-index: 9;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
    display: none;
  }
  .btnbox li a{
    display: block;
    padding: 7px 15px;
  }
  .btnbox li a:hover{
    background-color: #F5F5F5;
  }
</style>
<script>
layui.use('sale', layui.factory('sale')).use(['admin', 'sale', 'table'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form;

  layui.params = {};
  layui.morehover = false;
  $('.btnbox').mouseenter(function(){
    layui.morehover = true;
  });
  $('.btnbox').mouseleave(function(){
      $('.btnbox').fadeOut(100);
  });
  $('#lqjl').on('click', function(){
    $('#lqjl'+layui.params.id).click();
  });
  $('#cyyh').on('click', function(){
    $('#cyyh'+layui.params.id).click();
  });
  $('#hdsj').on('click', function(){
    $('#hdsj'+layui.params.id).click();
  });
  $('#cjspjl').on('click', function () {
    $('#cjspjl' + layui.params.id).click();
  });
  $('#sclb').on('click', function () {
    $('#sclb' + layui.params.id).click();
  });
  $('#jshd').on('click', function(){
    layer.confirm("确定结束活动吗？",function(i) {
        $.post(
            layui.setter.baseUrl+'sale/endsale',
            {id: layui.params.id},
            function(ret){
                var {code,msg,data} = ret;
                layer.msg(msg);
                if(code==0){
                    layui.table.reload('LAY-sale-manage');
                }
            }
        );
    })
  });
  
  form.render(null, 'layadmin-salefront-formlist');
  
  //监听搜索
  form.on('submit(LAY-user-front-search)', function(data){
    var params = [];
    for (let key in data.field) {
      params.push(key+'='+data.field[key]);
    }
    //执行重载
    table.reload('LAY-sale-manage', {
      url: layui.setter.baseUrl+"sale/index?"+params.join('&')
    });
  });

  //事件
  var active = {
    batchdel: function(){
      var checkStatus = table.checkStatus('LAY-sale-manage')
      ,checkData = checkStatus.data; //得到选中的数据

      if(checkData.length === 0){
        return layer.msg('请选择数据');
      }
      
      layer.confirm('确定删除吗？', function(index) {
        var ids = [];
        checkData.forEach((item,index)=>{
          ids.push(item.id);
        });
        $.post(
            layui.setter.baseUrl+'sale.del',
            {id: ids.join(',')},
            function(ret){
                var {code,msg,data} = ret;
                layer.alert(msg);
                if(code==0){
                    table.reload('LAY-sale-manage');
                }
            }
        );
      });
    }
    ,add: function(){
      admin.popup({
        title: '添加活动'
        ,area: [$(window).width()*0.618+'px', $(window).height()*0.9+'px']
        ,id: 'LAY-popup-sale-add'
        ,success: function(layero, index){
          view(this.id).render('sale/add');
        }
      });
    }
  };
  
  $('.layui-btn.layuiadmin-btn-sale').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
});
</script>